<?php

namespace App\Http\Controllers;

use App\TrainOrder;
use Illuminate\Http\Request;

class TrainOrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $trainOrder = TrainOrder::latest('created_at')->Paginate(8);
        return view('Train.trainOrder',compact('trainOrder'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $trainOrder=TrainOrder::findOrFail($id);
        return view('Train.editTrainOrder',compact('trainOrder'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $trainOrder=TrainOrder::findOrFail($id);
        $trainOrder->update([
            'username'=>$request->get('username'),
            'phone'=>$request->get('phone'),
            'professional'=>$request->get('professional'),
            'counts'=>$request->get('counts') 
        ]);
        $trainOrder->save();
        session()->flash('success',"信息更新");
        return redirect()->route('trainOrder.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $trainOrder = TrainOrder::find($id);
        $trainOrder->delete();

        session()->flash('success', '删除成功');
        return redirect()->route('trainOrder.index');
    }
}
